// Copyright 2013 Tumblr, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package vena

import (
	"sync"
)

type SpaceIndex struct {
	sync.Mutex
	fwd map[string]*TagIndex	// Metric name -> tag index
	rev map[MetricID]string		// Metric ID -> metric name
	n   MetricID
}

func NewSpaceIndex() *SpaceIndex {
	return &SpaceIndex{
		fwd: make(map[string]*TagIndex),
		rev: make(map[MetricID]string),
		n:   1,	// 0 is reserved for query stuff
	}
}

func (t *SpaceIndex) forward(metric string) *TagIndex {
	t.Lock()
	defer t.Unlock()
	tagIndex, ok := t.fwd[metric]
	if ok {
		return tagIndex
	}
	//
	if t.isCache {
		??
	}
	//
	if tagIndex == nil {
		metricID := t.n
		t.n++
		tagIndex = NewTagIndex(metricID)
		t.fwd[metric] = tagIndex
		t.rev[metricID] = metric
	}
	return tagIndex
}

func (t *SpaceIndex) Forward(metric string, tagset map[string]string) (MetricID, TagSet) {
	…
}

func (t *SpaceIndex) Reverse(metric string, tagset TagSet) map[string]string {
	…
}

type TagIndex struct {
	MetricID MetricID
	sync.Mutex
	fwd map[string]*KeyIndex	// Key name -> key index
	rev map[KeyID]string		// Key ID -> key name
	n   KeyID
}

func NewTagIndex(metricID MetricID) *TagIndex {
	return &TagIndex{
		MetricID: MetricID,
		fwd: make(map[string]*KeyIndex),
		rev: make(map[KeyID]string),
		n:   1,	// 0 is reserved for query stuff
	}
}

type KeyIndex struct {
	KeyID KeyID
	sync.Mutex
	fwd map[string]ValueID		// Value payload -> value ID
	rev map[ValueID]string		// Value ID -> value payload
	n   ValueID
}

func NewKeyIndex() *KeyIndex {
	return &KeyIndex{
		fwd: make(map[string]ValueID),
		rev: make(map[ValueID]string),
		n:   1,	// 0 is reserved for query stuff
	}
}
